<?php

require_once 'include/base.php';
#require_once 'libs/top/Taoapi.php';
require_once 'include/pagination.php';
require_once 'include/staticdata.php';
require_once 'include/topwrap.php';
require_once 'include/global.func.php';
$smarty = new TopSmartyBase();

$top_config = Taoapi_Config::Init();
$top_config->setCharset('UTF-8');

$taoapi = new TopWrap(false,true);
$cat_items = $taoapi->getItemCats();

$tmpcats   = $cat_items['item_cats']['item_cat'];
$count     = count($tmpcats);
for( $i=0; $i<$count; $i++ )
{
	$tmpcats[$i]['cat_url'] = url('list',array('catid'=>$tmpcats[$i]['cid']));
}
$smarty->getSmarty()->assign('item_cats',$tmpcats);
unset($tmpcats);
unset($cat_items);

$cat_items = $taoapi->getItemCats($_GET['catid']);
$tmpcats   = $cat_items['item_cats']['item_cat'];
$count     = count($tmpcats);
for( $i=0; $i<$count; $i++ )
{
	$tmpcats[$i]['cat_url'] = url('list',array('catid'=>$tmpcats[$i]['cid']));
}
$smarty->getSmarty()->assign('sub_item_cats',$tmpcats);
$smarty->getSmarty()->assign('empty_sub_cats',empty($cat_items['item_cats']['item_cat']));

/***
 * 获取指定catid的名称
 */

$cat = $taoapi->getItemCat($_GET['catid']);
$smarty->getSmarty()->assign('title',$cat['item_cats']['item_cat']['name']);

$sorts = StaticData::getSort();
$sort  = isset($_GET['sort']) ? intval($_GET['sort']) : 0;
$strSort = 'credit_desc';
if($sort <= 5)$strSort = $sorts[$sort][1];
$smarty->getSmarty()->assign('curnav', $sort);
$pageno=intval($_GET['page'])<=0 ? 1 : intval($_GET['page']);
$items = $taoapi->getItems($_GET['catid'],$pageno,21,$strSort);

$pagination = new Pagination($items['total_results']);
$smarty->getSmarty()->assign('catnav',gen_nav($_GET['catid'], &$taoapi));
//$smarty->getSmarty()->assign('pagination', $pagination->getPagination());
$smarty->getSmarty()->assign('pagination', $pagination->getPaginationRewrite());
$tmpitems = $items['taobaoke_items']['taobaoke_item'];

$count = count($tmpitems);
for($i = 0; $i < $count; $i++)
{
	$tmpitems[$i]['item_url'] = url('item',array('num_iid'=>$tmpitems[$i]['num_iid']));
}

$smarty->getSmarty()->assign('items',$tmpitems);
$smarty->getSmarty()->assign('get',$_GET);

if($count == 0)
{
    $keywords = "淘宝商品";
    $description = "该分类列表里没有数据";
}
else
{
    $keywords = $cat['item_cats']['item_cat']['name'];
    $description = $cat['item_cats']['item_cat']['name'] . '分类的列表,共100页,当前第'.$pageno.'页';
}
$smarty->getSmarty()->assign('kw',$_GET['kw']);
$smarty->getSmarty()->assign('count', $count);
$smarty->getSmarty()->assign('keywords',$keywords);
$smarty->getSmarty()->assign('description',$description);
$smarty->getSmarty()->display('list.tpl');


function get_key_desc($catid,$type='key')
{
	$data = StaticData::getMenuData();
	foreach ($data as $m)
	{
		if($m[2] == $catid)
		{
			if($type == 'key') {
				return $m[3];
			} else {
				return $m[4];
			}
		}
	}
}
